package at.jaki.ubb.crawling.demo.dataTransformer;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import at.jaki.ubb.crawling.api.DataTransformer;
import at.jaki.ubb.crawling.demo.model.StringEntity;
import at.jaki.ubb.crawling.model.DataTransformerResult;
import at.jaki.ubb.crawling.model.RawData;
import at.jaki.ubb.crawling.stlib.dataFinder.SimpleHttpClient;

public class PageClassifier implements DataTransformer<String, String, StringEntity, String> {

	public static final String TYPE_MEDIAFAX_SEARCH_PAGE = "mediafaxSearch";
	public static final String TYPE_MEDIAFAX_ARTICLE_PAGE = "mediafaxArticle";
	public static final String TYPE_REALITATEA_SEARCH_PAGE = "realitateaSearch";
	public static final String TYPE_REALITATEA_ARTICLE_PAGE = "realitateaArticle";
	public static final String TYPE_OTHER_PAGE = "otherPage";

	private static final String PATTERN_MEDIAFAX_SEARCH_PAGE = "[<]title[>][^>]* - Mediafax.ro[<][/]title[>][\\s\\S]*[<]div class[=][\"]box search[\"][>]";
	private static final String PATTERN_MEDIAFAX_ARTICLE_PAGE = "[<]title[>][^>]* - Mediafax[<][/]title[>][\\s\\S]*[<]div class[=][\"]news tabs-container[\"][>]";

	private static final String PATTERN_REALITATEA_SEARCH_PAGE = "[<]title[>]Cautare[:][\\s\\S]*[|] REALITATEA [.]NET[<][/]title[>]";

	public DataTransformerResult<String, String, StringEntity> transformData(RawData<String> rawData) {

		// the new raw data
		String type = getTypeOf(rawData.getData());
		RawData<String> newRd = new RawData<String>(rawData.getId().concat("C"), rawData.getData(), new Date(), type);

		Collection<RawData<String>> rawDataCollection = Collections.singletonList(newRd);

		DataTransformerResult<String, String, StringEntity> dtr = new DataTransformerResult<String, String, StringEntity>(rawDataCollection, null,
				null);
		return dtr;
	}

	private String getTypeOf(String data) {

		Pattern mediafaxSearchP = Pattern.compile(PATTERN_MEDIAFAX_SEARCH_PAGE);
		Matcher mediafaxSearchM = mediafaxSearchP.matcher(data);

		if (mediafaxSearchM.find()) {
			return TYPE_MEDIAFAX_SEARCH_PAGE;
		}

		Pattern mediafaxArticleP = Pattern.compile(PATTERN_MEDIAFAX_ARTICLE_PAGE);
		Matcher mediafaxArticleM = mediafaxArticleP.matcher(data);

		if (mediafaxArticleM.find()) {
			return TYPE_MEDIAFAX_ARTICLE_PAGE;
		}

		Pattern realitateaSearchP = Pattern.compile(PATTERN_REALITATEA_SEARCH_PAGE);
		Matcher realitateaSearchM = realitateaSearchP.matcher(data);

		if (realitateaSearchM.find()) {
			return TYPE_REALITATEA_SEARCH_PAGE;
		}
		return TYPE_OTHER_PAGE;
	}

	public void init() {
		// nothing to initialize
	}

	/**
	 * accepts data mined by the http miner
	 */
	public List<String> getTypes() {
		return Collections.singletonList(SimpleHttpClient.RESULT_TYPE);
	}

}
